#   Copyright 2006 Jerome Lacoste (jerome@coffeebreaks.org)
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
class AddTimezoneUserConfig < ActiveRecord::Migration
  def self.up
    add_column "users", "timezone_name", :string
    say_with_time "Updating user timezones..." do
      User.reset_column_information
      User.find(:all).each do |u|
        u.timezone_name = 'Europe/Oslo'
        u.update
      end
    end
    say_with_time "Updating worklogs timestamps to UTC..." do
      Worklog.find(:all).each do |w|
        user = w.worker
        w.done_at = user.tzget.local_to_utc(w.done_at)
        w.update
      end
    end
  end

  def self.down
    say_with_time "Updating worklogs timestamps to local value..." do
      Worklog.find(:all).each do |w|
        user = w.worker
        w.done_at = user.tzget.utc_to_local(w.done_at)
        w.update
      end
    end
    remove_column "users", "time_zone_name"
  end
end
